import pygame
import sys
from pygame.locals import *
import time
import pymunk as pm
import math
import os
from utils import show_text, is_rect


def loading():
    screen = pygame.display.set_mode((955, 537))
    init_background = pygame.image.load(
        os.path.join("images", "mainPicture.jpg")).convert_alpha()
    pygame.display.set_caption('AngryBirds')
    temp = 0
    flag = 0

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        screen.blit(init_background, (0, 0))
        text = u"Loading" + temp * u"."
        show_text(screen, (760, 425), text, (255, 255, 255), True)
        temp += 1
        if temp == 4:
            temp = 0
            flag += 1
            if flag == 4:
                time.sleep(2)
                return

        time.sleep(0.5)
        pygame.display.update()


def menu():
    screen = pygame.display.set_mode((896, 525))
    menu_background = pygame.image.load(
        os.path.join("images", "menu.jpg")).convert_alpha()
    pygame.display.set_caption('AngryBirds')

    # 按钮的坐标范围
    rect = (395, 315, 495, 365)

    # 背景音乐
    song1 = os.path.join('music', 'title_theme.wav')
    pygame.mixer.music.load(song1)
    pygame.mixer.music.play(-1)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # 回车或者鼠标点击指定范围开始游戏
            if event.type == KEYDOWN:
                if event.key == K_KP_ENTER:
                    return

            if event.type == pygame.MOUSEBUTTONDOWN:
                if is_rect(event.pos, rect):
                    return

        screen.blit(menu_background, (0, 0))

        pygame.display.update()


def draw_level_cleared(screen, level, pigs, score, game_state, bonus_score_once, bold_font3, bold_font2, WHITE, BLACK, star1, star2, star3, replay_button, next_button):
    """Draw level cleared"""
    music_flag = 0  # 胜利音效
    level_cleared = bold_font3.render("Level Cleared!", 1, WHITE)
    score_level_cleared = bold_font2.render(str(score), 1, WHITE)
    if level.number_of_birds >= 0 and len(pigs) == 0:
        if bonus_score_once:
            score += (level.number_of_birds - 1) * 10000
        bonus_score_once = False
        if game_state != 4:
            music_flag = 1
        game_state = 4

        music_flag += 1
        if music_flag == 2:
            song_win = os.path.join('music', 'level clear military a1.wav')
            pygame.mixer.music.load(song_win)
            pygame.mixer.music.play(0)
            music_flag = 0

        rect = pygame.Rect(300, 0, 600, 800)
        pygame.draw.rect(screen, BLACK, rect)
        screen.blit(level_cleared, (450, 90))
        if score >= level.one_star and score <= level.two_star:
            screen.blit(star1, (310, 190))
        if score >= level.two_star and score <= level.three_star:
            screen.blit(star1, (310, 190))
            screen.blit(star2, (500, 170))
        if score >= level.three_star:
            screen.blit(star1, (310, 190))
            screen.blit(star2, (500, 170))
            screen.blit(star3, (700, 200))
        screen.blit(score_level_cleared, (550, 400))
        screen.blit(replay_button, (510, 480))
        screen.blit(next_button, (620, 480))
    return game_state, bonus_score_once, score


def draw_level_failed(screen, level, pigs, t2, game_state, bold_font3, WHITE, BLACK, pig_happy, replay_button):
    """Draw level failed"""
    failed = bold_font3.render("Level Failed", 1, WHITE)
    if level.number_of_birds <= 0 and time.time() - t2 > 5 and len(pigs) > 0:
        music_flag = 0
        if game_state != 3:
            music_flag = 1
        game_state = 3

        music_flag += 1
        if music_flag == 2:
            song_win = os.path.join('music', 'level failed piglets a1.wav')
            pygame.mixer.music.load(song_win)
            pygame.mixer.music.play(0)
            music_flag = 0
        rect = pygame.Rect(300, 0, 600, 800)
        pygame.draw.rect(screen, BLACK, rect)
        screen.blit(failed, (450, 90))
        screen.blit(pig_happy, (380, 120))
        screen.blit(replay_button, (520, 460))
    return game_state